package ms.jacrim.tropos;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;

//import org.w3c.dom.NodeList;

public class Service extends TroposObject {

	public enum ServiceType {
		GOAL, SOFTGOAL, TASK, EVENT, RESOURCE
	};

	private static final String SERVICE_TEMPLATE = "<services xmi:type=\"%s\" xmi:id=\"%s\" name=\"%s\" serviceType=\"%s\" serviceProperties=\"%s\" />\r\n";
	private ServiceType serviceType;
	private String serviceProperties;
	private TroposObject parent;

	public Service(Diagram diagram) {
		super(diagram);
		setType(TroposObject.SERVICE);
		diagram.addService(this);
		serviceProperties = "";
	}

	// private Service(Diagram diagram, Service source) {
	// super(diagram, source);
	// serviceType = source.serviceType;
	// serviceProperties = source.serviceProperties;
	// }

	public Service(Diagram diagram, Element element) {
		super(diagram, element);
		diagram.addService(this);
	}

	@Override
	public void populate(Element element) {
		super.populate(element);
		serviceType = ServiceType.valueOf(element.getAttribute("serviceType"));
		serviceProperties = element.getAttribute("serviceProperties");
	}

	public void setServiceType(ServiceType serviceType) {
		this.serviceType = serviceType;
	}

	public ServiceType getServiceType() {
		return serviceType;
	}

	public void setServiceProperties(String serviceProperties) {
		this.serviceProperties = serviceProperties;
	}

	public String getServiceProperties() {
		return serviceProperties;
	}
	
	public TroposObject getParent() {
		return parent;
	}
	
	public void setParent(TroposObject value) {
		parent = value;
	}

	@Override
	public String toString() {
		return String.format(SERVICE_TEMPLATE, this.getType(), this.getId(), this.getName(), serviceType, serviceProperties);
	}

	@Override
	void toString(StringBuilder result, int part, int indent) {
		if (part == 0) {
			for (int i = 0; i < indent; i++)
				result.append("\t");
			result.append(String.format(SERVICE_TEMPLATE, this.getType(), this.getId(), this.getName(), serviceType,
					serviceProperties));
		}
		else {
			writeIndent(result, indent);
			result.append(String.format("<children xmi:type=\"notation:Node\" xmi:id=\"%s\" type=\"2001\" element=\"%s\">\r\n",
						getNotationID().toString(), getId()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<children xmi:type=\"notation:Node\" xmi:id=\"%s\" type=\"4001\"/>\r\n",
						UID.generate().toString()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<styles xmi:type=\"notation:FontStyle\" xmi:id=\"%s\" fontName=\"Segoe UI\"/>\r\n",
					UID.generate().toString()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<styles xmi:type=\"notation:DescriptionStyle\" xmi:id=\"%s\"/>\r\n",
					UID.generate().toString()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<styles xmi:type=\"notation:FillStyle\" xmi:id=\"%s\"/>\r\n",
					UID.generate().toString()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<styles xmi:type=\"notation:LineStyle\" xmi:id=\"%s\"/>\r\n",
					UID.generate().toString()));
			
			writeIndent(result, indent + 1);
			result.append(String.format("<layoutConstraint xmi:type=\"notation:Bounds\" xmi:id=\"%s\"/>\r\n",
					UID.generate().toString()));
			
			writeIndent(result, indent);
			result.append("</children>\r\n");
		}
	}

	public List<Composition> getCompositions() {
		ArrayList<Composition> result = new ArrayList<Composition>();
		for (TroposObject o : diagram.getObjects())
			if (o instanceof Composition && ((Composition) o).getSourceId().equalsIgnoreCase(this.getId()))
				result.add((Composition) o);
		return result;
	}
}
